package com.h3c.nem.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.h3c.nem.utils.AppBusinessException;
import lombok.Getter;

import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author chenyifan
 * @date 2022/6/6 13:38
 * 学历枚举类
 */
@Getter
public enum EducationLevelEnum {
    PRIMARY_SCHOOL("V0", "小学", -4),

    JUNIOR_HIGH_SCHOOL("V1", "初中", -3),

    HIGH_SCHOOL("V2", "高中", -2),

    TECHNICAL("V3", "中专", -1),

    COLLEGE("V4", "大专", 0),

    UNDERGRADUATE("V5", "本科", 1),

    MASTER("V6", "硕士", 2),

    PHD("V7", "博士", 3),

    POSTDOC("V8", "博士后", 4),

    MBA("Y9", "MBA", 5),

    COLLEGE_CORR("Y1", "大专（函授）", 0),

    COLLEGE_ING("Y2", "大专（在职）", 0),

    COLLEGE_OTH("Z1", "大专（其他）", 0),

    UNDERGRADUATE_CORR("Y3", "本科（函授）", 1),

    UNDERGRADUATE_ING("Y4", "本科（在职）", 1),

    UNDERGRADUATE_OTH("Z2", "本科（其他）", 1),

    MASTER_CORR("Y5", "硕士（函授）", 2),

    MASTER_ING("Y6", "硕士（在职）", 2),

    MASTER_OTH("Z3", "硕士（其他）", 2),

    PHD_CORR("Y7", "博士（函授）", 3),

    PHD_ING("Y8", "博士（在职）", 3),

    PHD_OTH("Z4", "博士（其他）", 3);


    @EnumValue
    @JsonValue
    private final String code;

    private final String typeName;

    private final Integer sort;

    EducationLevelEnum(String code, String typeName, Integer sort) {
        this.code = code;
        this.typeName = typeName;
        this.sort = sort;
    }

    /**
     * code 作为 key，封装为 Map
     */
    private static final Map<String, EducationLevelEnum> CODE_MAP = Stream
            .of(EducationLevelEnum.values())
            .collect(Collectors.toMap(EducationLevelEnum::getCode, Function.identity()));

    /**
     * 是否大于硕士
     *
     * @param levelEnum
     * @return
     */
    public static Boolean judgeIfMoreThanMaster(EducationLevelEnum levelEnum) {
        return levelEnum.getSort().compareTo(MASTER.getSort()) >= 0;
    }

    /**
     * 静态工厂反序列化
     *
     * @param key code
     * @return 评论的类型枚举
     */
    @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
    public static EducationLevelEnum valueOfKey(String key) {
        return Optional.ofNullable(CODE_MAP.get(key))
                .orElseThrow(() -> new IllegalArgumentException(String.valueOf(key)));
    }

    public static EducationLevelEnum getEnumByName(String name) {
        for (EducationLevelEnum value : EducationLevelEnum.values()) {
            if (value.typeName.equals(name)) {
                return value;
            }
        }
        throw new AppBusinessException("未找到EducationLevelEnum枚举：" + name);
    }
}
